package com.itheima.controller;


import com.alibaba.fastjson.JSON;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.constant.TokenConstant;
import com.itheima.entity.dto.PhoneDto;
import com.itheima.entity.vo.user.UserVo;
import com.itheima.service.UserService;
import com.itheima.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author huangzelnag
 * @create 2022-05-12
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    UserService userService;



    /**
     * 发送短信
     * @param phoneDto
     * @return
     */
    @PostMapping("sendMsg")
    public R UserSendMsg(@RequestBody PhoneDto phoneDto){
        int code = CodeUtils.makeAuthCode();
        System.out.println(code);
        System.out.println(phoneDto.getPhone());
        phoneDto.setCode(code);


        String jsonStr = JSON.toJSONString(phoneDto);

        String codeStr="code："+code;
        redisTemplate.opsForValue().set(phoneDto.getPhone(),jsonStr,1, TimeUnit.MINUTES);

        return R.error(codeStr);
    }


    /**
     * 用户登陆接口
     * @param PhoneDto
     * @return
     */
    @PostMapping("login")
    public R UserLogin(@RequestBody PhoneDto PhoneDto){
        String phone = PhoneDto.getPhone();

        String jsonStr = (String) redisTemplate.opsForValue().get(phone);


        PhoneDto redisPhone = JSON.parseObject(jsonStr, PhoneDto.class);

        if(Objects.isNull(redisPhone)){
            return R.error("验证码已过期,请重新输入");
        }
        Integer code = PhoneDto.getCode();
        System.out.println("=====PhoneDtocode====="+code);
        System.out.println("=====rediscode====="+redisPhone.getCode());

        if (!code.equals(PhoneDto.getCode())) {
            return R.success("验证码错误");
        }

        //获取用户信息
        UserVo user = userService.getUser(phone);
        if(Objects.isNull(user)){
            return R.error("错误");
        }
        String id = user.getId();

        String userToken  = System.currentTimeMillis()+id;
        userToken = DigestUtils.md5DigestAsHex(userToken.getBytes());
        user.setToken(userToken);


        String josnStr = JSON.toJSONString(user);

        redisTemplate.opsForValue().set(TokenConstant.USER_TOKEN_PREFIX +userToken,josnStr );



        return R.success(user);
    }

    /**
     * 退出登陆
     * @param req
     * @return
     */
    @PostMapping("loginout")
    public R logout(HttpServletRequest req){
        String token = req.getHeader("AuthorizationUser");
        userService.logout(token);
        return R.success("登出");
    }








}
